﻿package mortal.game.view.copy.defenseCopy.view
{
    import Message.DB.Tables.*;
    import __AS3__.vec.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.copy.defenseCopy.*;
    import mortal.game.view.interfaceCallModule.*;
    import mortal.mvc.core.*;

    public class DefenseCopyComposeView extends GSprite
    {
        private var _bg:GBitmap;
        private var _baseItem:BaseItem;
        private var _priceTxt:GTextFiled;
        private var _numTxt:GTextFiled;
        private var _state:GBitmap;
        private var _layer:int;
        private var _scale:int;
        private var _allItem:Vector.<DefenseCopyComposeView>;
        private var _itemCode:int;
        private var _packHasItem:Boolean;

        public function DefenseCopyComposeView()
        {
            this._allItem = new Vector.<DefenseCopyComposeView>;
            this.isHideDispose = true;
            return;
        }// end function

        public function setItemCode(param1:int, param2:int = 1, param3:int = 1, param4:int = 0) : void
        {
            var _loc_6:DefenseCopyComposeView = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            this._itemCode = param1;
            this._layer = param2;
            this._scale = param3;
            if (this._layer > Cache.instance.copy.defenseCopy.maxLayer)
            {
                Cache.instance.copy.defenseCopy.maxLayer = this._layer;
            }
            this._baseItem.itemCode = param1;
            UITrackPanelSub2.addCkArr(param1, this._baseItem.itemData.name, param2);
            var _loc_5:* = DefenseCopyComposeConfig.instance.getConfigByItemCode(param1);
            if (_loc_5 == null)
            {
                this.updatePrice();
                return;
            }
            _loc_10 = 1;
            while (_loc_10 <= DefenseCopyUtil.MAX_COMPOSE_MATERIAL)
            {
                
                _loc_7 = _loc_5["material" + _loc_10];
                if (_loc_7 > 0)
                {
                    _loc_9++;
                }
                _loc_10++;
            }
            _loc_10 = 1;
            while (_loc_10 <= DefenseCopyUtil.MAX_COMPOSE_MATERIAL)
            {
                
                _loc_7 = _loc_5["material" + _loc_10];
                _loc_8 = _loc_5["num" + _loc_10];
                if (_loc_7 > 0)
                {
                    _loc_6 = UIFactory.getUICompoment(DefenseCopyComposeView, 0, 105, this);
                    _loc_6.setItemCode(_loc_7, (this._layer + 1), _loc_9 * this._scale, _loc_8);
                    _loc_6.numTxt = _loc_8.toString();
                    this._allItem.push(_loc_6);
                }
                _loc_10++;
            }
            this.updatePrice();
            return;
        }// end function

        public function set numTxt(param1:String) : void
        {
            var _loc_2:* = DefenseCopyUtil.getItemNum(this._itemCode);
            if (_loc_2 >= int(param1))
            {
                this._numTxt.htmlText = _loc_2 + "/" + param1;
            }
            else
            {
                this._numTxt.htmlText = HTMLUtil.addColor(_loc_2.toString(), GlobalStyle.colorHong) + "/" + param1;
            }
            return;
        }// end function

        public function get numTxt() : String
        {
            return this._numTxt.text;
        }// end function

        public function get itemCode() : int
        {
            return this._itemCode;
        }// end function

        public function updatePos() : void
        {
            var _loc_3:int = 0;
            var _loc_4:DefenseCopyComposeView = null;
            var _loc_1:* = DefenseCopyUtil.getMaxLenByLayer(this._layer) / this._scale;
            var _loc_2:* = this._allItem.length;
            if (_loc_2 == 1)
            {
                DefenseCopyComposeView(this._allItem[0]).x = 0;
            }
            else if (_loc_2 == 2)
            {
                _loc_3 = _loc_1 / 2;
                DefenseCopyComposeView(this._allItem[0]).x = -_loc_3;
                DefenseCopyComposeView(this._allItem[1]).x = _loc_3;
            }
            else if (_loc_2 == 3)
            {
                _loc_3 = _loc_1 / 2;
                DefenseCopyComposeView(this._allItem[0]).x = -_loc_3;
                DefenseCopyComposeView(this._allItem[1]).x = 0;
                DefenseCopyComposeView(this._allItem[2]).x = _loc_3;
            }
            else if (_loc_2 == 4)
            {
                _loc_3 = _loc_1 / 4;
                DefenseCopyComposeView(this._allItem[0]).x = (-_loc_3) * 2;
                DefenseCopyComposeView(this._allItem[1]).x = -_loc_3;
                DefenseCopyComposeView(this._allItem[2]).x = _loc_3;
                DefenseCopyComposeView(this._allItem[3]).x = _loc_3 * 2;
            }
            for each (_loc_4 in this._allItem)
            {
                
                _loc_4.updatePos();
            }
            this.drawLine();
            return;
        }// end function

        public function updatePrice() : void
        {
            var _loc_1:DefenseCopyComposeView = null;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:String = null;
            if (this._itemCode > 0)
            {
                if (DefenseCopyUtil.getIsEnoughGem(this._itemCode))
                {
                    _loc_2 = DefenseCopyUtil.getPriceByCode(this._itemCode);
                    _loc_3 = Cache.instance.copy.defenseCopy.copyReward.equipPoint;
                    _loc_4 = _loc_3 >= _loc_2 ? (GlobalStyle.colorBai) : (GlobalStyle.colorHong);
                    this._priceTxt.htmlText = HTMLUtil.addColor(_loc_2.toString(), _loc_4);
                }
                else
                {
                    this._priceTxt.htmlText = HTMLUtil.addColor("" + Language.getString(71396), GlobalStyle.colorHong);
                }
                this.packHasItem = DefenseCopyUtil.getItemNum(this._itemCode) > 0;
            }
            for each (_loc_1 in this._allItem)
            {
                
                _loc_1.updatePrice();
            }
            return;
        }// end function

        public function set packHasItem(param1:Boolean) : void
        {
            this._packHasItem = param1;
            this._state.visible = param1;
            this._priceTxt.visible = !param1;
            return;
        }// end function

        public function get packHasItem() : Boolean
        {
            return this._packHasItem;
        }// end function

        public function getAllPackHasItem(param1:Array) : Array
        {
            var _loc_2:DefenseCopyComposeView = null;
            for each (_loc_2 in this._allItem)
            {
                
                if (_loc_2.packHasItem)
                {
                    param1.push(_loc_2);
                }
                _loc_2.getAllPackHasItem(param1);
            }
            return param1;
        }// end function

        public function updateAllPackHasItem() : void
        {
            var _loc_3:DefenseCopyComposeView = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_1:* = new Dictionary();
            var _loc_2:* = this.getAllPackHasItem([]);
            for each (_loc_3 in _loc_2)
            {
                
                _loc_4 = DefenseCopyUtil.getItemNum(_loc_3.itemCode);
                _loc_5 = int(_loc_1[_loc_3.itemCode]);
                if (_loc_4 - _loc_5 < int(_loc_3.numTxt))
                {
                    _loc_3.packHasItem = false;
                    continue;
                }
                _loc_1[_loc_3.itemCode] = int(_loc_1[_loc_3.itemCode]) + 1;
                _loc_3.packHasItem = true;
            }
            return;
        }// end function

        private function drawLine() : void
        {
            var _loc_2:DefenseCopyComposeView = null;
            var _loc_3:DefenseCopyComposeView = null;
            var _loc_4:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            this.graphics.clear();
            this.graphics.lineStyle(3, 4871516);
            var _loc_1:* = this._allItem.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_1)
            {
                
                if (_loc_1 == 1)
                {
                    _loc_3 = this._allItem[_loc_5] as DefenseCopyComposeView;
                    _loc_4 = _loc_3.x + DefenseCopyUtil.BG_LEN / 2;
                    this.graphics.moveTo(_loc_4, _loc_3.y);
                    this.graphics.lineTo(_loc_4, _loc_3.y - DefenseCopyUtil.LINE_H - 20);
                }
                else if (_loc_5 > 0)
                {
                    _loc_2 = this._allItem[(_loc_5 - 1)] as DefenseCopyComposeView;
                    _loc_3 = this._allItem[_loc_5] as DefenseCopyComposeView;
                    _loc_6 = _loc_2.x + DefenseCopyUtil.BG_LEN / 2;
                    _loc_7 = _loc_3.x + DefenseCopyUtil.BG_LEN / 2;
                    this.graphics.moveTo(_loc_6, _loc_2.y);
                    this.graphics.lineTo(_loc_6, _loc_2.y - DefenseCopyUtil.LINE_H);
                    this.graphics.lineTo(_loc_7, _loc_2.y - DefenseCopyUtil.LINE_H);
                    this.graphics.lineTo(_loc_7, _loc_2.y);
                }
                _loc_5++;
            }
            if (_loc_1 > 1)
            {
                _loc_2 = this._allItem[0] as DefenseCopyComposeView;
                _loc_3 = this._allItem[(_loc_1 - 1)];
                _loc_4 = (_loc_2.x + _loc_3.x + DefenseCopyUtil.BG_LEN) / 2;
                this.graphics.moveTo(_loc_4, _loc_2.y - DefenseCopyUtil.LINE_H);
                this.graphics.lineTo(_loc_4, _loc_2.y - DefenseCopyUtil.LINE_H * 2);
            }
            return;
        }// end function

        private function onClick(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyComposeUpdate, this._itemCode));
            return;
        }// end function

        private function onResCompl() : void
        {
            this._bg.bitmapData = GlobalClass.getBitmapData(ImagesConst.DefenseCopyShopItemBg);
            this._state.bitmapData = GlobalClass.getBitmapData(ImagesConst.DefenseCopyState);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.gBitmap("", 0, 0, this);
            this._baseItem = UIFactory.baseItem(4, 4, 35, 35, this);
            this._baseItem.configEventListener(MouseEvent.CLICK, this.onClick);
            this._priceTxt = UIFactory.gTextField("", 1, 36, 42, 20, this, GlobalStyle.textFormatPutong.center());
            this._priceTxt.mouseEnabled = false;
            this._numTxt = UIFactory.gTextField("", 1, 20, 42, 20, this, GlobalStyle.textFormatPutong.center());
            this._numTxt.mouseEnabled = false;
            this._state = UIFactory.gBitmap("", 14, 38, this);
            LoaderHelp.addResCallBack(ResFileConst.defenseCopy, this.onResCompl);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            var _loc_2:DefenseCopyComposeView = null;
            super.disposeImpl(param1);
            this._itemCode = 0;
            this._packHasItem = false;
            this._bg.dispose(param1);
            this._baseItem.dispose(param1);
            this._priceTxt.dispose(param1);
            this._numTxt.dispose(param1);
            this._state.dispose(param1);
            this._bg = null;
            this._baseItem = null;
            this._priceTxt = null;
            this._numTxt = null;
            this._state = null;
            for each (_loc_2 in this._allItem)
            {
                
                _loc_2.dispose(param1);
            }
            this._allItem.splice(0, this._allItem.length);
            return;
        }// end function

    }
}
